<template>
    <div>
    <Form
            ref="form"
          :form-items="formItems"
          :form-data="formData"
          :loading="formLoading"
    ></Form>
    <BaiduMapLocationDialog ref="baiduMapLocationDialog" v-on:selectLocation="(location)=>{
    $refs.form.setFormItem('longitude',location.longitude)
    $refs.form.setFormItem('latitude',location.latitude)
    }">
    </BaiduMapLocationDialog>
    </div>
</template>

<script>
    import Form from '../../../../common-util/src/components/page/Form.vue'
    import BaiduMapLocationDialog from '../../../../common-util/src/components/common/BaiduMapLocationDialog.vue'

    export default {
        components:{
            Form,
            BaiduMapLocationDialog
        },
        data(){
            return {
                formLoading: false,
                formData:null,
                formItems:[
                    {
                        field: {
                            name: 'code'
                        },
                        element:{
                            label: '编码',
                            disabled: true
                        }
                    },
                    {
                        field: {
                            name: 'name'
                        },
                        element:{
                            label: '名称',
                            required: true
                        }
                    },
                    {
                        field: {
                            name: 'typeDictId'
                        },
                        element:{
                            label: '类型',
                            type: 'selectDict',
                            selectDict:{
                                groupCode: 'area_type'
                            },
                            required: true
                        }
                    },
                    {
                        field: {
                            name: 'adminDivisionId'
                        },
                        element:{
                            label: '行政区划id',
                        }
                    },
                    {
                        field: {
                            name: 'longitude'
                        },
                        element:{
                            type: 'inputButton',
                            inputButton:{
                                bClick:this.longitudeBtnClick
                            },
                            label: '经度',
                        }
                    },
                    {
                        field: {
                            name: 'latitude'
                        },
                        element:{
                            label: '纬度',
                        }
                    },
                    {
                        field: {
                            name: 'seq'
                        },
                        element:{
                            type: 'inputNumber',
                            label: '排序',
                        }
                    },
                    {
                        field: {
                            name: 'remark'
                        },
                        element:{
                            type: 'textarea',
                            label: '备注',
                        }
                    },
                    {
                        field: {
                            name: 'version'
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: 'submit',
                                    requestMethod:'put',
                                    url: this.$baseURL.base +  '/base/area/' +this.$route.params.areaId,
                                    label: '保存'
                                },
                                {
                                    action: 'reset',
                                    label: '重置',
                                    type: 'default'
                                }
                            ]
                        }
                    }
                ]
            }
        },
        mounted(){
            this.getData(this.$route.params.areaId)
        },
        methods:{
            // 根据id获取数据
            getData(id){
                this.formLoading = true
                this.axios.get(this.$baseURL.base +  '/base/area/' + id).then(res => {
                    let data = res.data
                    this.formData = data
                }).catch(error => {
                    if(error.response){
                        if(error.response.status == 401){
                            return
                        }
                        if(error.response.status == 404){
                            this.$message.error('数据不存在，请刷新数据再试')
                        }else {
                            this.$message.error(error.response.data.msg)
                        }
                    }else {
                        this.$message.error('网络错误或服务不可用')
                    }
                }).finally(()=>{
                    this.formLoading = false
                })
            },
            longitudeBtnClick () {

                let form =this.$refs.form.getFormData()
                this.$refs.baiduMapLocationDialog.show(form.longitude,form.latitude)

            },
        }
    }
</script>

<style scoped>

</style>